import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Line2D;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.print.event.PrintJobAttributeEvent;
import javax.print.event.PrintJobAttributeListener;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import javax.print.event.PrintServiceAttributeEvent;
import javax.print.event.PrintServiceAttributeListener;
import javax.swing.ImageIcon;


public class Printer implements Printable,PrintServiceAttributeListener {

	private PrinterJob printerJob;
	private final Image imageLogo =new ImageIcon(getClass().getResource("images/print/logo.png")).getImage();
	private int print_count=0;
	private long[] page_value=null;
	public Printer(){
	  
		printerJob = PrinterJob.getPrinterJob();

		Paper paper = new Paper();
		paper.setSize(224, 730);
		paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
		PageFormat pageformat = new PageFormat();
		pageformat.setPaper(paper);
	  
		printerJob.setPrintable(this,pageformat);
		printerJob.getPrintService().addPrintServiceAttributeListener(this);
    
	}
  
	public void print(long[] page_value_,int print_count_){
	  
		print_count=print_count_;
		page_value=page_value_;
		//if (printerJob.printDialog()) {
		try{
			printerJob.print();
		}catch (Exception PrintException) {
			PrintException.printStackTrace();
		}
	  //}
	}
  
  
	public int print(Graphics g, PageFormat pageFormat, int page) {

		int ypos=0;
	    int xpos=0;
	    int GAP=2;
	    Graphics2D g2d;
	    String msg = null;
	    Line2D.Double line = new Line2D.Double();
	    char[] chr_spaces = null;
	    String str_spaces = null;
	    String content = null;
	    
	    long price = 0;
	    long sum = 0;
	    
	    NumberFormat numberFormat = NumberFormat.getNumberInstance();
	    
	    if (page == 0) {
    		g2d = (Graphics2D) g;
    		g2d.setColor(Color.black);
    		Font font = new Font("굴림체",Font.PLAIN,10);
    		
    		g2d.setFont(font);
    		//--- Translate the origin to be (0,0)
    		g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
			
    		g2d.drawImage(imageLogo, (int)(pageFormat.getWidth()/2-(imageLogo.getWidth(null)/2)), 0, null);
	        
    		ypos+=imageLogo.getHeight(null);
    		ypos+=font.getSize()+GAP;
    		msg = "노바디 노즈 코리아";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "서울시 광진구 화양동 건국대학교";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "[대표자] 김태욱 김혜빈";
    		g2d.drawString(msg, 0, ypos);
    		
    		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    		Date date = new Date();
    		
    		DecimalFormat decimalFormat = new DecimalFormat("000");
    		
    		ypos+=font.getSize()+GAP;
    		msg = "[일자] "+dateFormat.format(date)+"   [번호] "+decimalFormat.format(print_count);
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "[상품 정보]";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "성별 : "+Price_DB.getProductContent(page_value, 0)+" / 나이 : "+Price_DB.getProductContent(page_value, 1)+"대";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "=========================================";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "상 품 명          내 용           금 액";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "-----------------------------------------";
    		g2d.drawString(msg, 0, ypos);
    		
    		// 1.
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(2)+"         ";
    		
    		content=Price_DB.getProductContent(page_value, 2);
    		msg += Math_N.getRightString(content, 9)+"      ";
    		
    		price=Price_DB.getProductPrice(page_value, 2);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//2.
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(3)+"          ";
    		
    		content=Price_DB.getProductContent(page_value, 3);
    		msg += Math_N.getRightString(content, 7)+"       ";
    		
    		price=Price_DB.getProductPrice(page_value, 3);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//3
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(4)+"      ";
    		
    		content=Price_DB.getProductContent(page_value, 4);
    		msg += Math_N.getCenterString(content, 6)+"       ";
    				
    		price=Price_DB.getProductPrice(page_value, 4);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//4
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(5)+"      ";
    		
    		content=Price_DB.getProductContent(page_value, 5);
    		msg += Math_N.getCenterString(content, 6)+"       ";
    				
    		price=Price_DB.getProductPrice(page_value, 5);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//5
    		ypos+=font.getSize()+GAP;
			msg = Price_DB.getProductName(6);
    		
    		content=Price_DB.getProductContent(page_value, 6);
    		if(content.equals("아니오")){
    			msg +="           "+ Math_N.getCenterString(content, 6)+"       ";
				
    			price=0;
        		sum+=price;
        		content=numberFormat.format(price);
        		msg += Math_N.getRightString(content, 10);
        		g2d.drawString(msg, 0, ypos);
    		}else{
    			int row=0;
    			
    			for(int i=7;i<21;i++){
    				if(i!=8 && i!=11){
    					if(page_value[i]==1){
    						if(row++==0){
        						content = Price_DB.getCosmeticContent(i);
        						msg +="         "+ Math_N.getCenterString(content, 10)+"     ";
        						
        						price=Price_DB.getCosmeticPrice(i);
        			    		sum+=price;
        			    		content=numberFormat.format(price);
        			    		msg += Math_N.getRightString(content, 10);
        			    		g2d.drawString(msg, 0, ypos);
        					}else{
        						ypos+=font.getSize()+GAP;
        						msg = "       ";
        						content = Price_DB.getCosmeticContent(i);
        						msg +="        "+ Math_N.getCenterString(content, 10)+"     ";
        						
        						price=Price_DB.getCosmeticPrice(i);
        			    		sum+=price;
        			    		content=numberFormat.format(price);
        			    		msg += Math_N.getRightString(content, 10);
        			    		g2d.drawString(msg, 0, ypos);
        					}
    					}
    				}
    			}
    		}
    		
    		
    		//6
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(7)+"            ";
    		
    		content=Price_DB.getProductContent(page_value, 7);
    		msg += Math_N.getCenterString(content, 5)+"       ";
    				
    		price=Price_DB.getProductPrice(page_value, 7);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//7
    		ypos+=font.getSize()+GAP;
    		msg = Price_DB.getProductName(8)+"   ";
    		
    		content=Price_DB.getProductContent(page_value, 8);
    		msg += Math_N.getCenterString(content, 13)+"    ";
    				
    		price=Price_DB.getProductPrice(page_value, 8);
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 10);
    		g2d.drawString(msg, 0, ypos);
    		
    		//8
    		if(page_value[4]<5){
    			ypos+=font.getSize()+GAP;
        		msg = Price_DB.getProductName(9)+" ";
        			
        		content=Price_DB.getProductContent(page_value, 9);
        		msg += Math_N.getCenterString(content, 20)+" ";
        				
        		price=Price_DB.getProductPrice(page_value, 9);
        		sum+=price;
        		content=numberFormat.format(price);
        		msg += Math_N.getRightString(content, 10);
        		g2d.drawString(msg, 0, ypos);
    		}
    		
    		
    		//9
    		ypos+=font.getSize()+GAP;
    		if(page_value[4]<5){
    			msg = Price_DB.getProductName(10);
    		}else{
    			msg = "8 연봉";
    		}
    		
    		msg += "                   ";
    		
    		price=page_value[6];
    		sum+=price;
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 14);
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "-----------------------------------------";
    		g2d.drawString(msg, 0, ypos);
    		
    		font = new Font("굴림체",Font.BOLD,10);
    		g2d.setFont(font);
    		
    		
    		ypos+=font.getSize()+GAP;
    		msg = "공급가액"+"             ";
    		price=(long)(sum/10);
    		content=numberFormat.format(sum-price);
    		msg += Math_N.getRightString(content, 16);
    		
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "부가세"+"               ";
    		content=numberFormat.format(price);
    		msg += Math_N.getRightString(content, 16);
    		
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "합계"+"                 ";
    		content=numberFormat.format(sum);
    		msg += Math_N.getRightString(content, 16);
    		g2d.drawString(msg, 0, ypos);
    		
    		font = new Font("굴림체",Font.PLAIN,10);
    		g2d.setFont(font);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "-----------------------------------------";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "* 카드결제, 현금영수증 발급이 불가하여";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "영수증을 지참하셔도 반품 되지 않습니다.";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "(영수증 처리법 시행규칙 제 135조 2항)";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "* 포인트 적립, 사용 및 통신사 제휴할인이";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "적용되지 않습니다.";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "* 자세한 내용은 홈페이지 참조.";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "  (http://noknows.wordpress.com)";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "=========================================";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "            한국 행복지수.";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "     OECD 34개국 중 32위!(2012년)";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		msg = "   고객님 행복한 하루 되세요 ! *^O^*";
    		g2d.drawString(msg, 0, ypos);
    		
    		ypos+=font.getSize()+GAP;
    		ypos+=font.getSize()+GAP;
    		msg = "=========================================";
    		g2d.drawString(msg, 0, ypos);
    		
	        return (PAGE_EXISTS);
	    } else
	    	return (NO_SUCH_PAGE);
  }
  
  
  public void attributeUpdate(PrintServiceAttributeEvent psae){
	  System.out.println("attributeUpdate!");
	  System.out.println(psae.toString());
	  System.out.println(psae.getAttributes().toArray()[0].getName());
	  
	  
  }

}
